#ifndef BUSINESS_H_
#define BUSINESS_H_

#include <boost/program_options.hpp>
#include <string>
#include <iostream>
#include <fstream>
#include "IBusiness.h"

namespace coder
{

namespace po = boost::program_options;
	
class Business : public IBusiness
{
private:
	std::string _fileName;
	std::string _buffer;
public:
	Business(int argc, char **argv)
		:_fileName("Coder Minus"), _buffer("")
	{
		po::options_description desc("Allowed options");
		desc.add_options()("file", po::value<std::string>(), "input file");

		po::positional_options_description p;
		
		p.add("file", 1);
		
		po::variables_map vm;
		po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
		po::notify(vm);
		if (vm.count("file"))
		{
			_fileName = vm["file"].as<std::string>();
			loadFile();
		}
	}
	
	std::string getText() const
	{
		return _buffer;
	}
	
	std::string getFileName() const
	{
		return _fileName;
	}
	
	void setText(const std::string & text)
	{
	    _buffer = text;
	    saveFile();
	}
private:	
	void loadFile()
	{
		std::ifstream fileStream(_fileName.c_str(), std::ios_base::in);
		std::string line;
		while(std::getline(fileStream, line))
		{
			_buffer += line + "\n";
		}
	}
	
	void saveFile()
	{
		if(_fileName != "Coder Minus")
		{
			std::ofstream fileStream(_fileName.c_str(), std::ios_base::out);
		
			fileStream << _buffer.c_str();
		}
	}
};

}

#endif /*BUSINESS_H_*/
